unit umain;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Menus,
  Buttons, ExtCtrls, IniFiles;

type

  { TAdminForm }

  TAdminForm = class(TForm)
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    BitBtn3: TBitBtn;
    BitBtn4: TBitBtn;
    BitBtn5: TBitBtn;
    Panel1: TPanel;
    procedure BitBtn1Click(Sender: TObject);
    procedure BitBtn2Click(Sender: TObject);
    procedure BitBtn3Click(Sender: TObject);
    procedure BitBtn5Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  AdminForm: TAdminForm;
  INI : TINIFile;

implementation
uses udbsettings,frmusers, umodules;
{$R *.lfm}

{ TAdminForm }

procedure TAdminForm.FormCreate(Sender: TObject);
begin
  ForceDirectoriesUTF8(GetUserDir+'bsalary');
  INI := TINIFile.Create(GetUserDir+'bsalary'+DirectorySeparator+'settings.ini');
end;

procedure TAdminForm.FormDestroy(Sender: TObject);
begin
  INI.WriteString('MODULES','ADMIN',Application.ExeName);
  INI.Free;
end;

procedure TAdminForm.FormShow(Sender: TObject);
begin
  DBSettingsForm.IBConnection1.DatabaseName:=INI.ReadString('DB','FILE',SysToUTF8(GetUserDir)+'bsalary'+DirectorySeparator+'bsalary.fbd');;
  DBSettingsForm.IBConnection1.UserName:=INI.ReadString('DB','USER','sysdba');
  DBSettingsForm.IBConnection1.Password:=INI.ReadString('DB','PASSWORD','masterkey');
  if not INI.ReadBool('DB','LOCAL',false) then DBSettingsForm.IBConnection1.HostName:=INI.ReadString('DB','HOSTNAME','localhost');
  try
   DBSettingsForm.IBConnection1.Connected:=true;
   BitBtn2.Enabled:=true;
  except
   ShowMessage('Неможливо підключитись до БД! Переваірте налаштування!');
   BitBtn1Click(sender);
  end;
end;

procedure TAdminForm.BitBtn5Click(Sender: TObject);
begin
  INI.UpdateFile;
  Close;
end;

procedure TAdminForm.BitBtn1Click(Sender: TObject);
begin
  udbsettings.isOK:=false;
  DBSettingsForm.DBFileNameEdit.Text:=INI.ReadString('DB','FILE',SysToUTF8(GetUserDir)+'bsalary'+DirectorySeparator+'bsalary.fbd');;
  DBSettingsForm.UsernameEdit.Text:=INI.ReadString('DB','USER','sysdba');
  DBSettingsForm.PasswordEdit.Text:=INI.ReadString('DB','PASSWORD','masterkey');
  DBSettingsForm.LocalDBCheckBox.Checked:=INI.ReadBool('DB','LOCAL',false);
  DBSettingsForm.HostComboBox.Items.Clear;
   if not FileExistsUTF8(GetUserDir+'bsalary'+DirectorySeparator+'hosts.lst') then begin
    DBSettingsForm.HostComboBox.Items.Add('localhost');
    DBSettingsForm.HostComboBox.ItemIndex:=0;
   end else begin
    DBSettingsForm.HostComboBox.Items.LoadFromFile(GetUserDir+'bsalary'+DirectorySeparator+'hosts.lst');
    DBSettingsForm.HostComboBox.Text:=INI.ReadString('DB','HOSTNAME','localhost');
   end;
   DBSettingsForm.HostComboBoxExit(sender);
   DBSettingsForm.PageControl1.ActivePage:=DBSettingsForm.TabSheet1;
   DBSettingsForm.ShowModal;
  if udbsettings.isOK then begin
    INI.WriteString('DB','FILE',DBSettingsForm.DBFileNameEdit.Text);
    INI.WriteString('DB','HOSTNAME',DBSettingsForm.HostComboBox.Text);
    INI.WriteString('DB','USER',DBSettingsForm.UsernameEdit.Text);
    INI.WriteString('DB','PASSWORD',DBSettingsForm.PasswordEdit.Text);
    INI.WriteBool('DB','LOCAL',DBSettingsForm.LocalDBCheckBox.Checked);
    DBSettingsForm.HostComboBox.Items.SaveToFile(GetUserDir+'bsalary'+DirectorySeparator+'hosts.lst');
  end;
  if udbsettings.isDBConnected then BitBtn2.Enabled :=true;
end;

procedure TAdminForm.BitBtn2Click(Sender: TObject);
begin
  DBSettingsForm.SQLQuery1.Active:=false;
  DBSettingsForm.SQLQuery1.SQL.Clear;
  DBSettingsForm.SQLQuery1.SQL.Add('SELECT ID, username, pass from users');
  DBSettingsForm.SQLQuery1.Open;
  UsersForm.ShowModal;
end;

procedure TAdminForm.BitBtn3Click(Sender: TObject);
begin
  if INI.ReadString('MODULES','PRINTLIB','')='' then INI.WriteString('MODULES','PRINTLIB','');
  ModulesForm.ShowModal;
end;

end.

